//
// Created by sgyt on 2020/2/5.
// waitpid不等待

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc,char* argv[])
{
    int pid;
    pid = fork();

    if(pid < 0)//创建进程失败
    {
        perror("fork is error\n");
    } else if (pid > 0)//父进程
    {
        int status,res;
        while(1)
        {
            res = waitpid(0,&status,WNOHANG);
            if(res < 0)//没有子进程正在进行
            {
                printf("no child is exit!\n");
                break;
            }else if(res == 0)//有子进程正在执行
            {
                printf("子进程正在执行\n");
                sleep(1);
                continue;
            } else if (res > 0)
            {
                printf("子进程pid = [%d]\n",res);
                if(WIFEXITED(status))
                {
                    printf("子进程正常退出，status:[%d]\n",WEXITSTATUS(status));
                } else if(WIFSIGNALED(status))
                {
                    printf("子进程正常退出，status:[%d]\n",WTERMSIG(status));
                }
            }

        }

    } else if(pid == 0)//子进程
    {
        printf("child pid = [%d],ppid = [%d]\n",getpid(),getppid());
        sleep(20);
    }
    return 0;
}



